<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\api\model\Slideshow;
use app\api\model\SpeciallyClassify;
use app\api\model\ExamVenue;
use think\Config;
use app\api\model\PracticeRecord;
use app\api\model\UserChapter;
use app\api\model\UserAnswer;
use app\api\model\FaIntegralSignin;
use app\api\model\SignLog;
use app\common\model\User;
use think\Db;


class Integral extends  Api{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    #获取积分天数
    public function getIntegralSignin(){
        $data = $this->request->param();
        $userId = $this->auth->id;
        if (!$userId) {
            $this->error('请先登录！');
        }
        $userInfo = User::where('id',$userId)->find();
        $list = FaIntegralSignin::where('state',1)->order('sort','asc')->select();
        $signinNumber = SignLog::where('uid',$userInfo['id'])->count();
        $postData = [
            'list'=>$list,
            'signinNumber'=>$signinNumber,
        ];
        return json(['code'=>1, 'message'=>'获取成功','data'=>$postData]);
    }

    #获取日历
    public function getRli(){
        $data = $this->request->param();
        $year = $data['year'];
        $month = $data['month'];
        $userId = $this->auth->id;
        if (!$userId) {
            $this->error('请先登录！');
        }
        $userInfo = User::where('id',$userId)->find();
        if($data['month'] < 10){
           $month = '0' . $data['month']; 
        }
        $years = $year . '-' . $month;
        $firstDayOfMonth = strtotime($year . '-' . $month . '-01');
        $numDaysOfMonth = date('t', $firstDayOfMonth);
        $firstDayOfWeek = date('w', $firstDayOfMonth);
        $list = array();
        for($day = 1; $day <= $numDaysOfMonth; $day++){
           $list[]['time'] = $day;
        }

        foreach($list as $k=>$v){
            $list[$k]['signin'] = SignLog::where(array('uid'=>$userInfo['id'],'year'=>$years,'days'=>$v['time']))->count();
        }
        return json(['code'=>1, 'message'=>'获取成功','data'=>$list]);
    }

    #用户签到
    public function userSignin(){
        $data = $this->request->param();
        $userId = $this->auth->id;
        if (!$userId) {
            $this->error('请先登录！');
        }
        $userInfo = User::where('id',$userId)->find();
        $year = date('Y-m');
        $days = date('d');
        $qdInfo = SignLog::where(array('uid'=>$userInfo['id'],'year'=>$year,'days'=>$days))->find();
        if($qdInfo){
             return json(['code'=>2023, 'message'=>'今日已签到']);
        }
        Db::startTrans();
        try {
            $postData = [
                'uid'=>$userInfo['id'],
                'year'=>date('Y-m'),
                'days'=>date('d'),
            ];
            $SignLog = new SignLog;
            $SignLog->save($postData);
            #处理用户积分
            $signinNumber = SignLog::where('uid',$userInfo['id'])->count();
            $list = FaIntegralSignin::where('state',1)->order('sort','asc')->select();
            $signinInfo = '';
            foreach($list as $k=>$v){
                if($v['day'] == $signinNumber){
                    $signinInfo = $v;
                }
            }
            if($signinInfo){
                $userInfo->score = $userInfo['score'] + $signinInfo['price'];
                $userInfo->save();
            }
            $userInfo['avatar'] = 'https://' . $_SERVER['HTTP_HOST']  .$userInfo['avatar'];
            Db::commit();
            return json(['code'=>1, 'message'=>'签到成功！','data'=>$userInfo]);
        } catch (\Exception $e) {
            Db::rollback();
            return json(['code'=>2, 'message'=>'签到失败！']);
        }
    }
    
}
